package com.yiqi.lottery.feature.documentary.view.adapter;

import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.text.Editable;
import android.text.Html;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.suozhang.framework.component.http.RxDataProcessFactory;
import com.suozhang.framework.component.recyclerview.DividerItemDecoration;
import com.suozhang.framework.entity.bo.UserInfoBo;
import com.suozhang.framework.framework.AM;
import com.suozhang.framework.framework.BaseActivity;
import com.suozhang.framework.utils.AppUtil;
import com.suozhang.framework.utils.T;
import com.yiqi.lottery.R;
import com.yiqi.lottery.common.util.DateTool;
import com.yiqi.lottery.entity.bo.HallUserBo;
import com.yiqi.lottery.entity.bo.OrderDetailBo;
import com.yiqi.lottery.entity.enums.CreateOrderType;
import com.yiqi.lottery.feature.documentary.contract.PersonContract;
import com.yiqi.lottery.feature.documentary.dependencies.home.DaggerFollowComponent;
import com.yiqi.lottery.feature.documentary.dependencies.home.FollowPresenterModule;
import com.yiqi.lottery.feature.home.view.PayActivity;
import com.yiqi.lottery.feature.lottery.view.adapter.TableAdpter;
import com.yiqi.lottery.framework.api.LotteryApi;
import com.yiqi.lottery.framework.api.UserApi;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

public class LotteryDetailActivity extends BaseActivity implements PersonContract.View {


    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    @Inject

    PersonContract.Presenter mPresenter;

    @BindView(R.id.tv_documentary_person_focus)
    CheckBox mPersonFocus;
    @BindView(R.id.iv_documentary_person_head)
    ImageView mIvDocumentaryPersonHead;
    @BindView(R.id.tv_documentary_person_name)
    TextView mTvDocumentaryPersonName;

    @BindView(R.id.tv_personal_record)
    TextView mTvPersonalRecord;
    @BindView(R.id.tv_personal_jiangjin)
    TextView mTvPersonalJiangjin;
    @BindView(R.id.tv_personal_fans)
    TextView mTvPersonalFans;
    @BindView(R.id.tv_personal_record_7)
    TextView mTvPersonalRecord7;
    @BindView(R.id.tv_personal_jiangjin_7)
    TextView mTvPersonalJiangjin7;
    @BindView(R.id.tv_personal_daihong_7)
    TextView mTvPersonalDaihong7;
    @BindView(R.id.tv_gendan_detail_type)
    TextView mTvGendanDetailType;
    @BindView(R.id.tv_gendan_detail_person)
    TextView mTvGendanDetailPerson;

    @BindView(R.id.tv_gendan_detail_desc)
    TextView mTvGendanDetailDesc;
    @BindView(R.id.tv_gendan_detail_zigou)
    TextView mTvGendanDetailZigou;
    @BindView(R.id.tv_gendan_detail_qigou)
    TextView mTvGendanDetailQigou;
    @BindView(R.id.tv_gendan_detail_follow_num)
    TextView mTvGendanDetailFollowNum;
    @BindView(R.id.tv_gendan_detail_follow_fangshi)
    TextView mTvGendanDetailFollowFangshi;
    @BindView(R.id.tv_gendan_detail_follow_prize)
    TextView mTvGendanDetailFollowPrize;
    @BindView(R.id.tv_gendan_detail_follow_bili)
    TextView mTvGendanDetailFollowBili;
    @BindView(R.id.tv_gendan_detail_follow_bet)
    EditText mTvGendanDetailFollowBet;
    @BindView(R.id.tv_gendan_detail_follow_date)
    TextView mTvGendanDetailFollowDate;
    @BindView(R.id.rv_table)
    RecyclerView mRvTable;
    @BindView(R.id.btn_order_follow)
    Button mBtnOrderFollow;
    @BindView(R.id.tv_follow_intro)
    TextView mTvFollowIntro;
    @BindView(R.id.btn_gendan_view)
    LinearLayout mBtnGendanView;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_type)
    TextView tvType;
    @BindView(R.id.tv_yj)
    TextView tvYj;
    @BindView(R.id.tv_zjje)
    TextView tvZjje;
    @BindView(R.id.tv_qgje)
    TextView tvQgje;
    @BindView(R.id.tv_gdrq)
    TextView tvGdrq;
    @BindView(R.id.btn_bet_minus)
    ImageView btnBetMinus;
    @BindView(R.id.btn_bet_add)
    ImageView btnBetAdd;
    @BindView(R.id.edt_documentary_beishu)
    EditText edtDocumentaryBeishu;
    @BindView(R.id.btn_bet_20)
    TextView btnBet20;
    @BindView(R.id.btn_bet_50)
    TextView btnBet50;
    @BindView(R.id.bottom_view)
    LinearLayout bottomView;
    @BindView(R.id.tv_xuanyan)
    TextView tvXuanyan;
    @BindView(R.id.tv_detail_balance)
    TextView mTvDetailBalance;
    @BindView(R.id.tv_detail_amount)
    TextView mTvDetailAmount;
    @BindView(R.id.tv_show_stop)
    TextView mTvShowStop;
    @BindView(R.id.tv_lottery_info)
    TextView mTvLotteryInfo;

    private Integer id;
    private int mBet = 1;

    private OrderDetailBo mDetailBo;
    private TableAdpter mAdapter;

    private Integer mUserId;

    private int mPerNum;

    @Override
    protected void initInjector() {
        DaggerFollowComponent.builder().followPresenterModule(new FollowPresenterModule(this)).build().inject(this);
    }

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_lottery_detail;
    }

    @Override
    protected void initData() {

        mUserId = this.getIntent().getIntExtra("user_id", 0);
        mPresenter.getUserInfo(mUserId);

    }

    @Override
    protected void initView() {
        initAdapter();
        initToolBar(mToolbar, "跟单详情", true, true);
        id = this.getIntent().getIntExtra("id", 0);
//        mPresenter.getGendanDetail(id, 2);
        AM.api().createApiService(LotteryApi.class).getGendanDetail(id, 2)
                .compose(RxDataProcessFactory.dataPrepAndIoToMainTransformer()).subscribe(new Observer<OrderDetailBo>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(OrderDetailBo detailBo) {
                mDetailBo = detailBo;
                mPerNum = detailBo.getBet_info().getPour_num();
                tvName.setText(detailBo.getBet_label() + "\t\t" + detailBo.getPass_type());
                tvType.setText(detailBo.getIs_open() == 1 ? "公开" : detailBo.getIs_open() == 2 ? "截止后可见" : "保密");
                tvYj.setText("10%");
                tvZjje.setText(detailBo.getOrder_amount() + "元");
                tvQgje.setText((mPerNum * 2 * mBet) + "元");
                tvGdrq.setText(detailBo.getFollow_num() + "人");
                tvXuanyan.setText(detailBo.getFollow_intro());
                if (detailBo.getBet_plan() != null && detailBo.getBet_plan().size() > 0) {
                    LayoutInflater mInflater = LayoutInflater.from(LotteryDetailActivity.this);
                    LinearLayout contentView = (LinearLayout) mInflater.inflate(R.layout.item_lottery_hall_title, null);
                    mAdapter.addHeaderView(contentView);
                    mAdapter.setNewData(detailBo.getBet_plan());
                }

                mTvDetailAmount.setText(Html.fromHtml("需要支付<font color=\"#FB5950\">" + (mPerNum * 2 * mBet) + "元"));
                bottomView.setVisibility(detailBo.getFollow_status() == 0 ? View.VISIBLE : View.GONE);
                mTvShowStop.setText("截至时间" + DateTool.TimeStamp2Date((detailBo.getFinish_time()) + ""));
                if (detailBo.getIs_open() == 1) {
                    mRvTable.setVisibility(View.VISIBLE);
                    mTvShowStop.setVisibility(View.GONE);
                    mTvLotteryInfo.setText("1.佣金-中奖后所得奖金0%\n2.起跟金额-发单人自行设置起跟金额\n3.自购金额-发单人自己购买的金额");
                } else if (detailBo.getIs_open() == 2) {
                    if (DateTool.compare_date(detailBo.getFinish_time(), System.currentTimeMillis()) == 1) {
                        mRvTable.setVisibility(View.VISIBLE);
                        mTvShowStop.setVisibility(View.GONE);
                    } else {
                        mRvTable.setVisibility(View.GONE);
                        mTvShowStop.setVisibility(View.VISIBLE);
                    }
                    mTvLotteryInfo.setText("1.佣金-中奖后所得奖金10%\n2.起跟金额-发单人自行设置起跟金额\n3.自购金额-发单人自己购买的金额");
                } else if (detailBo.getIs_open() == 3) {
                    mRvTable.setVisibility(View.GONE);
                    mTvShowStop.setVisibility(View.VISIBLE);
                    mTvLotteryInfo.setText("1.佣金-中奖后所得奖金10%\n2.起跟金额-发单人自行设置起跟金额\n3.自购金额-发单人自己购买的金额");
                }

            }

            @Override
            public void onError(Throwable e) {
                T.showShort(e.getMessage());
            }

            @Override
            public void onComplete() {

            }
        });


        AM.api().createApiService(UserApi.class)
                .getUserInfo()
                .compose(RxDataProcessFactory.dataPrepAndIoToMainTransformer())
                .subscribe(new Observer<UserInfoBo>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(UserInfoBo userInfoBo) {
                        if (userInfoBo != null) {
                            mTvDetailBalance.setText(Html.fromHtml("账户余额<font color=\"#FB5950\">" + userInfoBo.getBalance_amount() + "元"));
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    @Override
    public void showFollowSuccess(String id) {

    }

    @Override
    protected void initSystemParams() {
//        super.initSystemParams();
        AppUtil.fullScreen(this);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    }

    @Override
    protected void initEvent() {
        edtDocumentaryBeishu.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (!TextUtils.isEmpty(s.toString())) {
                    mBet = Integer.valueOf(s.toString());
                } else {
                    mBet = 0;
                }
                mTvDetailAmount.setText(Html.fromHtml("需要支付<font color=\"#FB5950\">" + (mPerNum * 2 * mBet) + "元"));
            }
        });
    }

    @Override
    public void showFollowError(String ex) {
        showErrorMsg(ex);
    }

    @Override
    public void showFollowOrderSuccess(String msg) {
        showMsg("提交成功！");
        finish();
    }

    @Override
    public void showUserInfoSuccess(HallUserBo info) {
        AM.image().bindToCircleObject(info.getAvatar(), mIvDocumentaryPersonHead);
        mTvDocumentaryPersonName.setText(info.getUsername());
        mTvPersonalRecord.setText(info.getRecord_amount());
        mTvPersonalJiangjin.setText("奖金" + info.getIncome_amount());
        mTvPersonalFans.setText("砖粉" + info.getFriend_num());
        mTvPersonalRecord7.setText(info.getSeven_record_amount());
        mTvPersonalJiangjin7.setText(info.getSeven_income_amount() + "");
        mTvPersonalDaihong7.setText(info.getSeven_follow_num() + "");


    }

    @Override
    public void showUserInfoError(String ex) {
        T.showShort("" + ex);
    }


    @Override
    public void showGendanDetailSuccess(OrderDetailBo detailBo) {

    }

    @OnClick(R.id.btn_order_follow)
    public void onViewClicked() {
        if (mBet > 200000 || mBet < 1) {
            T.showShort("跟单倍数为1-200000倍！");
            return;
        }

        Intent intent = new Intent(this, PayActivity.class);
        intent.putExtra("order_id", id);
        intent.putExtra("multiple", mBet);
        intent.putExtra("order_type", CreateOrderType.GD.getIndex());
        intent.putExtra("total_amount", (mPerNum * 2 * mBet));
        startActivity(intent);
    }

    private void initAdapter() {

        mAdapter = new TableAdpter();
        mRvTable.setLayoutManager(new LinearLayoutManager(this));
        mRvTable.addItemDecoration(new DividerItemDecoration(DividerItemDecoration.VERTICAL).setHeight((int) 0.2));
        mAdapter.bindToRecyclerView(mRvTable);

    }


    @OnClick({R.id.btn_documentary_jian, R.id.btn_documentary_jia, R.id.btn_bet_20, R.id.btn_bet_50})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_documentary_jian:
                if (mBet > 1) {
                    mBet--;
                }
                break;
            case R.id.btn_documentary_jia:
                mBet++;
                break;
            case R.id.btn_bet_20:
                mBet = 20;
                break;
            case R.id.btn_bet_50:
                mBet = 50;
                break;
        }

        edtDocumentaryBeishu.setText("" + mBet);
        mTvDetailAmount.setText(Html.fromHtml("需要支付<font color=\"#FB5950\">" + (mPerNum * 2 * mBet) + "元"));
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
